<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /*
       
      */
      // setTimeout(() => {
      //   console.log(1);
      //   setTimeout(() => {
      //     console.log(2);
      //     setTimeout(() => {
      //       console.log(3);
      //     }, Math.random() * 1000);
      //   }, Math.random() * 1000);
      // }, Math.random() * 1000);

      /*
        then 
        ① then(回调函数) 的返回值就是 then() 整个方法返回值
        ② then(回调函数) 默认返回 new Promise() 对象
      */
      const p = new Promise((resolve, reject) => {
        setTimeout(() => {
          console.log(1);
          resolve();
        }, Math.random() * 1000);
      });
      p.then(() => {
        return new Promise((resolve) => {
          setTimeout(() => {
            console.log(2);
            resolve();
          }, Math.random() * 1000);
        });
      })
        .then(() => {
          return new Promise((resolve) => {
            setTimeout(() => {
              console.log(3);
              resolve();
            }, Math.random() * 1000);
          });
        })
        .then(() => {
          return new Promise((resolve) => {
            setTimeout(() => {
              console.log(4);
              resolve();
            }, Math.random() * 1000);
          });
        });
    </script>
  </body>
</html>
